<template>
    <div style="display: inline-block" v-if="this.flag">
        <!--{{ createTime | timestampToTime }}-->
        <span style="font-size: 14px;color: #666;">剩余支付时间（超时自动关闭）</span>
        <span style="color: #F56C6C"><i class="el-icon-time"></i> {{ min }}分钟 {{sec}}秒 </span>
    </div>
</template>

<script>
    export default {
        name: 'CountDown',
        props: ['createTime'],
        data() {
            return {
                day: '',
                hr: '',
                min: '',
                sec: '',
                flag: true,
            }
        },
        methods: {
            //倒计时
            countdown () {
                let end = Date.parse(new Date(this.createTime))
                let now = Date.parse(new Date())
                //console.log("end =",new Date(end  +3000000), "now =",new Date(now))

                end = Date.parse(new Date(end  +1800000))

                let msec = end - now

                //console.log(end, now, msec)

                //console.log(msec)
                if(msec<0) {
                    this.flag = false
                    return;
                }

                let day = parseInt(msec / 1000 / 60 / 60 / 24)
                let hr = parseInt(msec / 1000 / 60 / 60 % 24)
                let min = parseInt(msec / 1000 / 60 % 60)
                let sec = parseInt(msec / 1000 % 60)
                this.day = day
                this.hr = hr > 9 ? hr : '0' + hr
                this.min = min > 9 ? min : '0' + min
                this.sec = sec > 9 ? sec : '0' + sec
                const that = this
                if(min>=0 && sec>=0){
                    //倒计时结束关闭订单
                    if(min === 0 && sec === 0){
                        return
                    }
                    setTimeout(function () {
                        that.countdown()
                    }, 1000)
                }
            }

        },
        filters: {
            // 时间戳转换成时间
            timestampToTime (timestamp) {
                let date = new Date(timestamp) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
                let Y = date.getFullYear() + '-'
                let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-'
                let D = date.getDate() + ' '
                let h = date.getHours() + ':'
                let m = date.getMinutes() + ':'
                let s = date.getSeconds()
                s = s < 10 ? '0' + s : s;
                return Y+M+D+h+m+s;
            },
        },
        mounted () {
            this.countdown()
        }
    }
</script>

<style>

</style>
